package data

import (
	"go_private_im/internal/logic/biz"
	"context"
	"github.com/go-kratos/kratos/v2/errors"
)


func (l *LogicRepo) DelRoomDetail(ctx context.Context,roomId string,userId int64) (err error) {
	roomInfo := biz.RoomDetailInfo{}
	if err = l.data.Db.Table("room_detail").Where("room_id = ? and user_id = ?",roomId,userId).Delete(&roomInfo).Error; err != nil {
		return errors.InternalServer("删除失败","删除失败")
	}
	return
}
func (l *LogicRepo) DelRoomDetails(ctx context.Context,roomId string,userId []int64) (err error) {
	roomInfo := []*biz.RoomDetailInfo{}
	if err = l.data.Db.Table("room_detail").Where("room_id = ? and user_id in ?",roomId,userId).Delete(&roomInfo).Error; err != nil {
		return errors.InternalServer("删除失败","删除失败")
	}
	return
}
func (l *LogicRepo) GetRoomDetail(ctx context.Context,roomId string,userId int64) (roomInfo *biz.RoomDetailInfo,err error) {
	roomInfo = &biz.RoomDetailInfo{}
	if err = l.data.Db.Table("room_detail").Where("room_id = ? and user_id = ? ",roomId,userId).Take(roomInfo).Error; err != nil {
		return
	}
	return
}
func (l *LogicRepo) GetRoomDetails(ctx context.Context,roomId string,userId []string) (roomInfo []*biz.RoomDetailInfo,err error) {
	roomInfo = []*biz.RoomDetailInfo{}
	if err = l.data.Db.Table("room_detail").Where("room_id = ? and user_id in ? ",roomId,userId).Find(&roomInfo).Error; err != nil {
		return
	}
	return
}
func (l *LogicRepo) GetRoomDetailList(ctx context.Context,roomId string) (roomInfo []*biz.RoomDetailInfo,err error) {
	roomInfo = []*biz.RoomDetailInfo{}
	if err = l.data.Db.Table("room_detail").Where("room_id = ?",roomId).Find(&roomInfo).Error; err != nil {
		return
	}
	return
}
func (l *LogicRepo) GetRoomDetailListByUserId(ctx context.Context,userId int64) (roomInfo []*biz.RoomDetailInfo,err error) {
	roomInfo = []*biz.RoomDetailInfo{}
	if err = l.data.Db.Table("room_detail").Where("user_id = ?",userId).Find(&roomInfo).Error; err != nil {
		return
	}
	return
}

func (l *LogicRepo) AddRoomDetail(ctx context.Context,roomDetails []*biz.RoomDetailInfo) error {
	if err := l.data.Db.Table("room_detail").Create(roomDetails).Error; err != nil{
		return err
	}
	return nil
}
func (l *LogicRepo) UpdataRoomDetail (ctx context.Context,roomDetail *biz.RoomDetailInfo) error {
	if err := l.data.Db.Table("room_detail").Updates(roomDetail).Error; err != nil{
		return err
	}
	return nil
}
func (l *LogicRepo) UserExistRoomDetail(ctx context.Context,roomId string,userId int64) bool  {
	if _,err := l.GetRoomDetail(ctx,roomId,userId); err != nil {
		return  false
	}
	return  true
}
